package com.example.dubbo.account.core.service;

import com.example.dubbo.account.api.AccountService;
import com.example.dubbo.account.core.dao.AccountMapper;
import com.example.dubbo.account.core.dao.data.AccountDO;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

@Slf4j
@DubboService
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountMapper accountMapper;

    @Override
    public void debit(String userId, int money) {
        AccountDO accountDO = accountMapper.getByUserId(userId);
        Preconditions.checkArgument(accountDO != null, "账户不存在");
        Preconditions.checkArgument(accountDO.getMoney() - money >= 0, "账户余额不足");
        int i = accountMapper.debitById(accountDO.getId(), money);
        log.info("accountMapper.debitById, userId={}, money={}, i={}", userId, money, i);
    }
}
